import json

from django.shortcuts import render
from django.http import  HttpResponse
from django.http import  JsonResponse
from Test.auto_selenium import login_auto
from  django.views.decorators.csrf  import  csrf_exempt
from Test.auto_selenium import login_auto

'''
# 方案一 ： 有一个进度条-过程页面。未实现 不知道如何让把参数显示在html页面上。
# Create your views here.
def login(request):
    if request.method == 'GET':
        return render(request,'login.html')
    username = request.POST.get('username')
    password = request.POST.get('password')
    # print('账户名：',username,'密码：',password)
    return render(request,'process.html',{'username':username,'password':password})

@csrf_exempt
def check_in(request):
    print(request.POST)
    print('------------------')
    rtncodemsg = {'code' : 400 ,'msg' : '用户名密码不为空'}
    return HttpResponse(json.dumps(rtncodemsg))
'''

# 方案二 ：直接在login页面，ajax 弹出显示

def auto_login(request):
    # if request.method == 'GET':
    return render(request,'auto_login.html')

@csrf_exempt
def auto_process(request):
    username = request.POST.get('username')
    password = request.POST.get('password')

    print(username,'----',password)
    if not username or not password:
        res = {'code':400,'msg':'账号密码为空，请重新输入'}
        return  JsonResponse(res)
    res = login_auto(username,password) # 注意 这里返回的是{} ，一定要在views中 return  JsonResponse(res)
    return  JsonResponse(res)









#ajax
def ajax(request):
    return render(request,'ajax.html')

@csrf_exempt  # 免除csrf认证
def ajax_test(request):
    id = request.POST.get('id')
    psw = request.POST.get('psw')
    # return HttpResponse('返回的值')
    print(id,psw)
    data={'id':id,'psw':psw}
    return JsonResponse(data) #相当于json.dumps

def jupyter(request):
    return render(request, 'jupyter.html')
